// pages/oftenline/oftenline.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city: '请选择',
    routeData:[
    
    ],
    totalPage: 1,
    currentPage: 1,
    ishome:0,
    isadd:1
  },
  update:function(e){
    var index = e.currentTarget.dataset.index;
    var temp = this.data.routeData[index];
    var that = this;
    util.commonRequest('updateLine', 'post', temp, function (res) {
      if (res.data.code == 200) {
        wx.showToast({
          title: res.data.msg
        })
        that.setData({
          routeData:[],
          currentPage: 1,
        })
        that.initData()
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  changecity: function (cityName, type,index) {
    if (type == 0) {
      var temp = this.data.routeData;
      temp[index]['route_origin'] = cityName;
      this.setData({
        routeData: temp
      })
    }
    else if (type == 1) {
      var temp = this.data.routeData;
      temp[index]['route_end'] = cityName;
      this.setData({
        routeData: temp
      })
    }
  },
  // 
  // 选择地址
  chose_city: function (e) {
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    var that = this;
    wx.navigateTo({
      url: '../address/address?type=' + type+"&index="+index,
    })
  },
  deleteFun:function(id){
    var that = this;
  
    util.commonRequest('deleteLine', 'post', { id: id }, function (res) {
      if (res.data.code == 200) {
        wx.showToast({
          title: res.data.msg
        })
        that.setData({
          routeData: [],
          currentPage: 1,
        })
        that.initData()
      } else {
        wx.showToast({
          icon: 'success',
          title: res.data.msg,
        })
      }
    });
  },
  //添加常跑路线
  addoftenline(){
    app.navTo('../addoftenline/addoftenline')
  },
  start_delete(e){
    var index = e.currentTarget.dataset.index;
    var temp = this.data.routeData[index];
    var that = this;
 
    wx.showModal({
      title: '温馨提示:',
      content: '您确定删除该路线吗?删除后无法恢复',
      success: function (res) {
        console.log(res);
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {

          that.deleteFun(temp['id']);
        }
      },
    })

   
    
  },
  selLine:function(e){
    var index = e.currentTarget.dataset.index;
    var temp = this.data.routeData[index];
    var that = this;
    if(this.data.ishome == 1){
      wx.setStorageSync('cpline', temp);
      wx.navigateBack({})
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ishome = options.ishome;
    if(ishome == 1){
      this.setData({
        ishome:1
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   routeData:[],
    //   currentPage:1
    this.initData()
    // })
    
  },
  initData:function()
  {
    var that = this;
    wx.showLoading();
    util.commonRequest('oftenLine', 'post', { currentPage: that.data.currentPage}, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        var tempArr = data.data.data;
        var oriArr = that.data.routeData;
        var finalyArr = tempArr;
        that.setData({
          routeData: finalyArr,
          totalPage: data.data.last_page
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
        that.setData({ isadd:0})
        // wx.navigateBack({})
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.initData();
    } else {
      console.log("已经是全部数据")
      // wx.showToast({
      //   'icon':'loading',
      //   title: '已经是全部数据了',
      // })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})